﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using RuleCode.Table;

namespace map.manage
{
    public partial class configshop : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //AliApi.appUserId = "maxfara@163.com"; //测试用
            if (!IsPostBack)      
            {
                dealrequest();
                if (AliApi.appUserId == "")
                {
                    HyperLink_login.Text = "登录";
                    HyperLink_login.NavigateUrl = ResolveUrl("~/member/login.aspx") + "?redirecturl=" + this.Request.Url;

                    HyperLink_logout.NavigateUrl = ResolveUrl("~/member/regist1.aspx") + "?redirecturl=" + this.Request.Url;
                    HyperLink_logout.Text = "注册";
                }
                else
                {
                    HyperLink_login.Text = AliApi.appNickName;
                    HyperLink_login.NavigateUrl = "#";
                    HyperLink_logout.NavigateUrl = ResolveUrl("~/member/loginout.aspx") + "?redirecturl=" + this.Request.Url;
                    HyperLink_logout.Text = "注销";
                }
                
                bind1();
                bind2();
                bind3();
                bind4();

               
            }
            Label_cur.Text = RadioButtonList1.SelectedValue;
        }
        private void dealrequest()
        {
            cbuser.dealcookie();
            if (AliApi.appUserId == "")
            {
                Response.Redirect(ResolveUrl("~/member/login.aspx") + "?redirecturl=" + this.Request.Url);
            }
        }

        /// <summary>
        /// 店铺信息
        /// </summary>
        private void bind1()
        {
            cbuser.dealcookie();
            cbshop s = cbshop.Find(" cbuserid = '" + AliApi.appUserId + "' ");
            TextBox1.Text = s.cbshoptitle.ToString();
            RadioButtonList3.SelectedValue = s.cbshoppagecss.ToString();
            this.Page.Title = s.cbshoptitle.ToString() + "-" + this.Page.Title;

            cbuser u = cbuser.Find(" cbuserid = '" + AliApi.appUserId + "'");
            HyperLink_openshop.NavigateUrl = ResolveUrl("~/member/default.aspx")
                + "?cbshoppageid=0&id=" + u.RECID.ToString();
        }
        /// <summary>
        /// 店铺页面
        /// </summary>
        private void bind2()
        {
            cbshoppage sp =cbshoppage.findorcreate(AliApi.appUserId,RadioButtonList1.Text);
            RadioButtonList2.Text = sp.cblanmu.ToString();
            TextBox_backcolour.Text = sp.backcolour.ToString();
            TextBox_wordcolour.Text = sp.wordcolour.ToString();
            bind3();
            RadioButtonList2_SelectedIndexChanged(null, EventArgs.Empty);
        }
        /// <summary>
        /// 框架 
        /// </summary>
        private void bind3()
        {
            string ScriptTag =
                    "<script type=\"text/javascript\">setsrc('../member/configshop_inner1.aspx?cbshoppageid="
                    + RadioButtonList1.SelectedIndex.ToString() + "');</script>";
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
                            "messagedw1", ScriptTag, false);
        }
        /// <summary>
        /// 绑定css数据
        /// </summary>
        private void bind4()
        {
            cbshopcss sc = cbshopcss.findorcreate(AliApi.appUserId);
            TextBox_allbackcolor.Text = sc.allbackcolor.ToString();
            TextBox_alltablecolor.Text = sc.alltablecolor.ToString();
            TextBox_allc1width.Text = sc.allc1width.ToString();
            TextBox_allc2width.Text = sc.allc2width.ToString();
            TextBox_menutablebordercolor.Text = sc.menutablebordercolor.ToString();
            TextBox_menutdhascontentwordcolor.Text = sc.menutdhascontentwordcolor.ToString();
            TextBox_menutdactivewordcolor.Text = sc.menutdactivewordcolor.ToString();
            TextBox_titlewordcolor.Text = sc.titlewordcolor.ToString();
            TextBox_titlebordercolor.Text = sc.titlebordercolor.ToString();
            TextBox_divbackcolor.Text = sc.cbname4.ToString();
        }

        //页面布局
        //        <asp:ListItem>首页</asp:ListItem>
        //<asp:ListItem>分类页</asp:ListItem>
        //<asp:ListItem>商品页</asp:ListItem>
        //<asp:ListItem>内容页</asp:ListItem>
        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            bind2();
        }

        ////页面布局
        //<asp:ListItem>三栏式</asp:ListItem>
        //        <asp:ListItem>左边框</asp:ListItem>
        //        <asp:ListItem>右边框</asp:ListItem>
        //        <asp:ListItem>无边框</asp:ListItem>
        protected void RadioButtonList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (RadioButtonList2.SelectedValue == "三栏式")
            {
                Image3.ImageUrl = ResolveUrl(@"~/img/lanmu_all.gif");
            }
            if (RadioButtonList2.SelectedValue == "左边框")
            {
                Image3.ImageUrl = ResolveUrl(@"~/img/lanmu_left.gif");
            }
            if (RadioButtonList2.SelectedValue == "右边框")
            {
                Image3.ImageUrl = ResolveUrl(@"~/img/lanmu_right.gif");
            }
            if (RadioButtonList2.SelectedValue == "无边框")
            {
                Image3.ImageUrl = ResolveUrl(@"~/img/lanmu_none.gif");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            cbshop s = cbshop.findorcreate(AliApi.appUserId);
            s.cbshoptitle.Value = TextBox1.Text;
            s.Modify();
        }
        
        protected void Button3_Click(object sender, EventArgs e)
        {
            string strImg2 = FileUpload3.FileName.Substring(FileUpload3.FileName.LastIndexOf(".") + 1);
            string time2 = DateTime.Now.ToString("yyyyMMddhhmmss") + "." + strImg2;
            string strFilePth = Server.MapPath(@"~/file/") + time2;
            string linkpth = ResolveUrl(@"~/file/") + time2;
            if (FileUpload3.HasFile)
            {
                FileUpload3.SaveAs(strFilePth);
                cbshop s = cbshop.findorcreate(AliApi.appUserId);
                s.cbpath0.Value = linkpth;
                s.Modify();
                MessageBox.Show(this.Page, "上传成功");
                return;
            }
            MessageBox.Show(this.Page, "上传失败");

        }
        protected void Button4_Click(object sender, EventArgs e)
        {
            cbshop s = cbshop.findorcreate(AliApi.appUserId);
            s.cbshoptitle.Value = "";
            s.Modify();
            MessageBox.Show(this.Page, "操作完成");
        }
        protected void Button5_Click(object sender, EventArgs e)
        {
            cbshop s = cbshop.findorcreate(AliApi.appUserId);
            s.cbpath0.Value = "";
            s.Modify();
            MessageBox.Show(this.Page, "操作完成");
        }
        /// <summary>
        /// 选择主题
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void RadioButtonList3_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbshop s = cbshop.findorcreate(AliApi.appUserId);
            s.cbshoppagecss.Value = RadioButtonList3.SelectedValue;
            s.Modify();
        }

        protected void Button6_Click(object sender, EventArgs e)
        {
            cbshoppage sp = cbshoppage.findorcreate(AliApi.appUserId, RadioButtonList1.Text);
            sp.backcolour.Value = TextBox_backcolour.Text.Trim();
            sp.wordcolour.Value = TextBox_wordcolour.Text.Trim();
            sp.Modify();
        }
        protected void Button7_Click(object sender, EventArgs e)
        {
            string strImg2 = FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf(".") + 1);
            string time2 = DateTime.Now.ToString("yyyyMMddhhmmss") + "." + strImg2;
            string strFilePth = Server.MapPath(@"~/file/") + time2;
            string linkpth = ResolveUrl(@"~/file/") + time2;
            if (FileUpload1.HasFile)
            {
                FileUpload1.SaveAs(strFilePth);
                cbshoppage sp = cbshoppage.findorcreate(AliApi.appUserId, RadioButtonList1.Text);
                sp.backpicpath.Value = linkpth;
                sp.Modify();
                MessageBox.Show(this.Page, "上传成功");
            }
            MessageBox.Show(this.Page, "上传失败");


        }
        protected void Button8_Click(object sender, EventArgs e)
        {

            cbshoppage sp = cbshoppage.findorcreate(AliApi.appUserId, RadioButtonList1.Text);
            sp.backpicpath.Value = "";
            sp.Modify();
        }
        //保存css
        protected void Button4_Click_save(object sender, EventArgs e)
        {
            cbshopcss sc = cbshopcss.findorcreate(AliApi.appUserId);
            sc.allbackcolor.Value = TextBox_allbackcolor.Text;
            sc.alltablecolor.Value = TextBox_alltablecolor.Text;
            sc.allc1width.Value = TextBox_allc1width.Text;
            sc.allc2width.Value = TextBox_allc2width.Text;
            sc.menutablebordercolor.Value = TextBox_menutablebordercolor.Text;
            sc.menutdhascontentwordcolor.Value = TextBox_menutdhascontentwordcolor.Text;
            sc.menutdactivewordcolor.Value = TextBox_menutdactivewordcolor.Text;
            sc.titlewordcolor.Value = TextBox_titlewordcolor.Text;
            sc.titlebordercolor.Value = TextBox_titlebordercolor.Text;
            sc.cbname4.Value = TextBox_divbackcolor.Text;
            sc.Modify();

            string strImg2 = FileUpload_alltoppic.FileName.Substring(FileUpload_alltoppic.FileName.LastIndexOf(".") + 1);
            string time2 = DateTime.Now.ToString("yyyyMMddhhmmss") + "." + strImg2;
            string strFilePth = Server.MapPath(@"~/file/") + time2;
            string linkpth = ResolveUrl(@"~/file/") + time2;
            if (FileUpload_alltoppic.HasFile)
            {
                FileUpload_alltoppic.SaveAs(strFilePth);
                sc = cbshopcss.findorcreate(AliApi.appUserId);
                sc.alltoppic.Value = linkpth;
                sc.Modify();
            }

            strImg2 = FileUpload_menutdbackpic.FileName.Substring(FileUpload_menutdbackpic.FileName.LastIndexOf(".") + 1);
            time2 = DateTime.Now.ToString("yyyyMMddhhmmss") + "." + strImg2;
            strFilePth = Server.MapPath(@"~/file/") + time2;
            linkpth = ResolveUrl(@"~/file/") + time2;
            if (FileUpload_menutdbackpic.HasFile)
            {
                FileUpload_menutdbackpic.SaveAs(strFilePth);
                sc = cbshopcss.findorcreate(AliApi.appUserId);
                sc.menutdbackpic.Value = linkpth;
                sc.Modify();
            }

            strImg2 = FileUpload_menutdhascontentbackpic.FileName.Substring(FileUpload_menutdhascontentbackpic.FileName.LastIndexOf(".") + 1);
            time2 = DateTime.Now.ToString("yyyyMMddhhmmss") + "." + strImg2;
            strFilePth = Server.MapPath(@"~/file/") + time2;
            linkpth = ResolveUrl(@"~/file/") + time2;
            if (FileUpload_menutdhascontentbackpic.HasFile)
            {
                FileUpload_menutdhascontentbackpic.SaveAs(strFilePth);
                 sc = cbshopcss.findorcreate(AliApi.appUserId);
                sc.menutdhascontentbackpic.Value = linkpth;
                sc.Modify();
            }

            strImg2 = FileUpload_menutdactivebackpic.FileName.Substring(FileUpload_menutdactivebackpic.FileName.LastIndexOf(".") + 1);
            time2 = DateTime.Now.ToString("yyyyMMddhhmmss") + "." + strImg2;
            strFilePth = Server.MapPath(@"~/file/") + time2;
            linkpth = ResolveUrl(@"~/file/") + time2;
            if (FileUpload_menutdactivebackpic.HasFile)
            {
                FileUpload_menutdactivebackpic.SaveAs(strFilePth);
                 sc = cbshopcss.findorcreate(AliApi.appUserId);
                sc.menutdactivebackpic.Value = linkpth;
                sc.Modify();
            }

            strImg2 = FileUpload_titlebackpic.FileName.Substring(FileUpload_titlebackpic.FileName.LastIndexOf(".") + 1);
            time2 = DateTime.Now.ToString("yyyyMMddhhmmss") + "." + strImg2;
            strFilePth = Server.MapPath(@"~/file/") + time2;
            linkpth = ResolveUrl(@"~/file/") + time2;
            if (FileUpload_titlebackpic.HasFile)
            {
                FileUpload_titlebackpic.SaveAs(strFilePth);
                 sc = cbshopcss.findorcreate(AliApi.appUserId);
                sc.titlebackpic.Value = linkpth;
                sc.Modify();
            }
            MessageBox.Show(this.Page , "操作完成。");
        }

        protected void Button9_Click(object sender, EventArgs e)
        {
            cbshopcss sc = cbshopcss.findorcreate(AliApi.appUserId);
            sc.alltoppic.Value = "";
            sc.Modify();
        }

        protected void Button13_Click(object sender, EventArgs e)
        {
            cbshopcss sc = cbshopcss.findorcreate(AliApi.appUserId);
            sc.menutdbackpic.Value = "";
            sc.Modify();
        }

        protected void Button12_Click(object sender, EventArgs e)
        {
            cbshopcss sc = cbshopcss.findorcreate(AliApi.appUserId);
            sc.menutdhascontentbackpic.Value = "";
            sc.Modify();
        }

        protected void Button11_Click(object sender, EventArgs e)
        {
            cbshopcss sc = cbshopcss.findorcreate(AliApi.appUserId);
            sc.menutdactivebackpic.Value = "";
            sc.Modify();
        }

        protected void Button10_Click(object sender, EventArgs e)
        {
            cbshopcss sc = cbshopcss.findorcreate(AliApi.appUserId);
            sc.titlebackpic.Value = "";
            sc.Modify();
        }

        protected void Button15_Click(object sender, EventArgs e)
        {
            cbshoppage sp = cbshoppage.findorcreate(AliApi.appUserId, RadioButtonList1.Text);
            sp.cblanmu.Value = RadioButtonList2.Text;
            sp.Modify();
            bind3();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string sql = " exec [复制店铺样式] ";
            if (RadioButtonList3.SelectedValue == "红色主题")
            {
                sql += " 'red@rulecode.com' ";
            }
            if (RadioButtonList3.SelectedValue == "蓝色主题")
            {
                sql += " 'blue@rulecode.com' ";
            }
            if (RadioButtonList3.SelectedValue == "绿色主题")
            {
                sql += " 'green@rulecode.com' ";
            }
            if (RadioButtonList3.SelectedValue == "金色主题")
            {
                sql += " 'gold@rulecode.com' ";
            }
            if (RadioButtonList3.SelectedValue == "灰色主题")
            {
                sql += " 'gray@rulecode.com' ";
            }

            sql += ", '"+AliApi.appUserId+"' ";
            RuleCode.Common.SqlHelper.Instance.ExecuteNonQuery(CommandType .Text , sql);
            MessageBox.Show(this.UpdatePanel1, "操作完成。");
        }

        protected void Button14_Click(object sender, EventArgs e)
        {
            string sql = " exec [复制店铺内容] ";
            if (RadioButtonList4.SelectedValue == "店铺模板1")
            {
                sql += " 'shop2@rulecode.com' ";
            }


            sql += ", '" + AliApi.appUserId + "' ";
            RuleCode.Common.SqlHelper.Instance.ExecuteNonQuery(CommandType.Text, sql);
            MessageBox.Show(this.Page, "操作完成。");
        }


    }
}
